﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using DataAccess.Repository;
using DataAccess.Models;

namespace KPI.Controls
{
	public partial class GroupUsers : System.Web.UI.UserControl
	{
		private IGroupRepository _GroupRepository;
		private List<Group> _listGroups;

		public List<Group> ListGroups
		{
			get
			{
				if(_listGroups == null)
				{
					if(Cache["allgroups"] == null)
					{
						Cache.Add("allgroups", GetGroups(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(4, 0, 0), System.Web.Caching.CacheItemPriority.Default, null);
					}

					_listGroups = Cache["allgroups"] as List<Group>;
				}

				return _listGroups;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			BindGroupCategory();
		}


		protected void rptGroupCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			var data = e.Item.DataItem as GroupCategory;

			if(data != null)
			{
				var lblGroupCategory = e.Item.FindControl("lblGroupCategory") as Label;
				var rptGroup = e.Item.FindControl("rptGroup") as Repeater;

				lblGroupCategory.Text = data.GroupCategoryName;

				rptGroup.ItemDataBound += new RepeaterItemEventHandler(rptGroup_ItemDataBound);

				rptGroup.DataSource = GetGroupByCategory(data.GroupCategoryID);
				rptGroup.DataBind();
			}
		}

		protected void rptGroup_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			var data = e.Item.DataItem as Group;

			if(data != null)
			{
				var lGroupName = e.Item.FindControl("lGroupName") as Literal;
				var hfGroupID = e.Item.FindControl("hfGroupID") as HiddenField;

				lGroupName.Text = data.GroupDispName;
				hfGroupID.Value = data.GroupID.ToString();
			}
		}

		protected void Page_UnLoad(object sender, EventArgs e)
		{
			if(_GroupRepository != null)
			{
				((IDisposable)_GroupRepository).Dispose();
			}
		}

		private List<Group> GetGroupByCategory(int categoryID)
		{
			List<Group> result = null;

			if(categoryID > 0 && ListGroups != null && ListGroups.Count > 0)
			{
				var groups = ListGroups.Where(g => g.GroupCategoryID == categoryID);

				if(groups != null)
				{
					result = groups.ToList();
				}
			}

			return result;
		}

		private void BindGroupCategory()
		{
			if(ListGroups != null && ListGroups.Count > 0)
			{
				List<GroupCategory> lstGroupCategory = new List<GroupCategory>();

				foreach(var item in ListGroups)
				{
					if(item.TheGroupCategory != null && lstGroupCategory.Count(gc => gc.GroupCategoryID == item.TheGroupCategory.GroupCategoryID) == 0)
					{
						lstGroupCategory.Add(item.TheGroupCategory);
					}
				}

				rptGroupCategory.DataSource = lstGroupCategory;
				rptGroupCategory.DataBind();
			}
		}

		private List<Group> GetGroups()
		{
			if(_GroupRepository == null)
			{
				_GroupRepository = new GroupRepository();
			}

			return _GroupRepository.GetAllGroups();
		}
	}
}